package com.stockz.equity.charting.decorator;

import java.awt.Rectangle;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.stockz.equity.charting.event.EventPublisher;
import com.stockz.equity.charting.event.SetStockChartPanelSizeEvent;
import com.stockz.equity.charting.view.StockChart;
import com.stockz.equity.charting.view.StockChartPanelState;
import com.stockz.equity.charting.view.StockChartPanelStructure;
import com.stockz.equity.model.Bar;
import com.stockz.equity.pattern.Decorator;

@Component
public class StockChartPanelLayoutDecorator implements Decorator<StockChart> {
	
	@Autowired private StockChartPanelIndicatorLayoutDecorator stockChartPanelIndicatorLayoutDecorator;
	
	@Override
	public void decorate(StockChart stockChart) {
		StockChartPanelState state = stockChart.getStockChartPanel().getStockChartPanelState();
		StockChartPanelStructure structure = stockChart.getStockChartPanel().getStockChartPanelStructure();
		
		Rectangle visibleRectangle = stockChart.getStockChartPanel().getVisibleRect();

		EventPublisher.publishEvent(new SetStockChartPanelSizeEvent(stockChart));
				
		state.getDrawingRectangle().setBounds(visibleRectangle.x + structure.getAxisPriceWidth(),
				visibleRectangle.y + structure.getAxisInformationHeight(),
				visibleRectangle.width - structure.getAxisPriceWidth() - structure.getAxisVolumeWidth(),
				visibleRectangle.height - structure.getAxisInformationHeight() - structure.getAxisTimeHeight());
		
		state.getVolumeRectangle().setBounds(state.getDrawingRectangle());
		state.getChartRectangle().setBounds(state.getDrawingRectangle());
		
		state.getPriceRectangle().setBounds(state.getDrawingRectangle().x,
				state.getDrawingRectangle().y+structure.getVoidSpaceTopHeight(),
				state.getDrawingRectangle().width,
				state.getDrawingRectangle().height-structure.getVoidSpaceBottomHeight()-structure.getVoidSpaceTopHeight());

		
		
		if(state.getBarList() != null){
			int endIndex = state.getBarList().size() - visibleRectangle.x/state.getTimeUnitSpace();
			if(endIndex < 0){
				endIndex = 0;
			}
			state.setEndIndex(endIndex);
			
			int startIndex = endIndex - state.getDrawingRectangle().width/state.getTimeUnitSpace();
			if(startIndex < 0){
				startIndex = 0;
			}
			state.setStartIndex(startIndex);
			
			state.setMinimumPriceVisible(Double.MAX_VALUE);
			state.setMinimumVolumeVisible(Double.MAX_VALUE);
			state.setMaximumPriceVisible(Double.MIN_VALUE);
			state.setMaximumVolumeVisible(Double.MIN_VALUE);
			
			if(startIndex == 0 && endIndex == 0){
				state.setMinimumPriceVisible(0);
				state.setMinimumVolumeVisible(0);
				state.setMaximumPriceVisible(0);
				state.setMaximumVolumeVisible(0);
			}
			
			for(int i = startIndex; i <= endIndex; i++){
				if(i >= state.getBarList().size() || i < 0){
					continue;
				}
				Bar bar = state.getBarList().get(i);
				if(state.getMinimumPriceVisible() > bar.getLow()){
					state.setMinimumPriceVisible(bar.getLow());
				}
				if(state.getMaximumPriceVisible() < bar.getHigh()){
					state.setMaximumPriceVisible(bar.getHigh());
				}
				if(state.getMinimumVolumeVisible() > bar.getVolume()){
					state.setMinimumVolumeVisible(bar.getVolume());
				}
				if(state.getMaximumVolumeVisible() < bar.getVolume()){
					state.setMaximumVolumeVisible(bar.getVolume());
				}
			}
		}
		
		stockChartPanelIndicatorLayoutDecorator.decorate(stockChart);
	}
}